package com.zl.basic.service.impl;

import com.zl.basic.constant.VerifyConstants;
import com.zl.basic.dto.SmsDto;
import com.zl.basic.exception.BusinessException;
import com.zl.basic.service.IVerifyCodeService;
import com.zl.basic.util.StrUtils;
import com.zl.basic.util.VerifyCodeUtils;
import com.zl.user.domain.User;
import com.zl.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Service
public class VerifyCodeServiceImpl implements IVerifyCodeService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Override
    public String getImageCode(String key) {
        if (key==null){
            throw new BusinessException("key无效");
        }
        String code = StrUtils.getComplexRandomString(4);
        redisTemplate.opsForValue().set(key,code,5, TimeUnit.MINUTES);
        String base64Str = VerifyCodeUtils.verifyCode(115, 42, code);
        return base64Str;
    }



    @Override
    public void sendSmsCode(SmsDto smsDto) {

        String imageCode = smsDto.getImageCode();
        String imageCodeKey = smsDto.getImageCodeKey();
        String phone = smsDto.getPhone();
        // 1.空值校验
        if (StringUtils.isEmpty(phone)){
            throw new BusinessException("手机号码不能为空!!!");
        }
        if (StringUtils.isEmpty(imageCode)){
            throw new BusinessException("图形验证码不能为空!!!");
        }
        if (StringUtils.isEmpty(imageCodeKey)){
            throw new BusinessException("请重新获取图形验证码!!!");
        }

        // 2图形验证码校验-是否过期
        Object redisImageCode = redisTemplate.opsForValue().get(imageCodeKey);
        if (redisImageCode==null){
            throw new BusinessException("图形验证码失效");
        }

        // 3图形验证码校验-是否正确
        if (!imageCode.equalsIgnoreCase(redisImageCode.toString())){
            throw  new BusinessException("图形验证码错误");
        }

        // 4 校验手机号是否已经注册
        User user = userMapper.loadByPhone(phone);
        if (user!=null){
            throw new BusinessException("手机号已经被注册，请直接登录！！！");
        }

        // 5 获取手机验证码
        Object redisSmsCode = redisTemplate.opsForValue().get("register:" + phone);
        String code = null;
        if (redisSmsCode==null){
            code=StrUtils.getComplexRandomString(6);
        }else{
            long redisTime = Long.parseLong(redisSmsCode.toString().split(":")[1]);
            long nowTime = System.currentTimeMillis();
            if ((nowTime-redisTime)<1*60*1000){
                throw new BusinessException("请勿重复获取验证码！！！");
            }else{

                code = redisSmsCode.toString().split(":")[0];
            }
        }

        // 7将验证码存入redis
        redisTemplate.opsForValue().set("register:"+phone,code+":"+System.currentTimeMillis(),3,TimeUnit.MINUTES);

        //8.调用SmsUtils发送短信
        System.out.println("手机验证码为:"+code+",请在3分钟内使用!!!");

    }

    @Override
    public void binderSmsCode(SmsDto smsDto) {
        String phone = smsDto.getPhone();
        // 1.空值校验
        if (StringUtils.isEmpty(phone)){
            throw new BusinessException("手机号码不能为空!!!");
        }

        // 5 获取手机验证码
        Object redisSmsCode = redisTemplate.opsForValue().get(VerifyConstants.BINDER_PREFIX + phone);
        String code = null;
        if (redisSmsCode==null){
            code=StrUtils.getComplexRandomString(6);
        }else{
            long redisTime = Long.parseLong(redisSmsCode.toString().split(":")[1]);
            long nowTime = System.currentTimeMillis();
            if ((nowTime-redisTime)<1*60*1000){
                throw new BusinessException("请勿重复获取验证码！！！");
            }else{

                code = redisSmsCode.toString().split(":")[0];
            }
        }

        // 7将验证码存入redis
        redisTemplate.opsForValue().set(VerifyConstants.BINDER_PREFIX+phone,code+":"+System.currentTimeMillis(),3,TimeUnit.MINUTES);

        //8.调用SmsUtils发送短信
        System.out.println("手机验证码为:"+code+",请在3分钟内使用!!!");
    }


}
